package cn.e4j.modular.crm.controller.base;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.common.exception.BizExceptionEnum;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.LangUtils;
import cn.e4j.model.exception.ServiceException;
import cn.e4j.modular.crm.entity.GweaT;
import cn.e4j.modular.crm.service.impl.Gwei001ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 税率税种控制类
 *
 * @author yuan_rz
 * @Date 2019/01/09 14:40
 */
@Controller
@RequestMapping("/base/gwei001")
public class Gwei001Controller extends BaseController {

    @Autowired
    private Gwei001ServiceImpl gwei001Service;

    /**
     * 跳转页面
     */
    @RequiresPermissions("base:gwei001:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/base/gwei001.html";
    }

    /**
     * 查询列表数据
     */
    @RequestMapping("/getListData")
    @RequiresPermissions("base:gwei001:init")
    @ResponseBody
    public ResponseData getListData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwei001Service.getListData(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 导出Excel
     */
    @RequestMapping(value="/exportListData",method=RequestMethod.GET)
    @RequiresPermissions("base:gwei001:init")
    public void exportListData(ModelMap map, HttpServletRequest request, HttpServletResponse response){

    }

    /**
     * 添加数据
     */
    @RequestMapping("/addData")
    @RequiresPermissions("base:gwei001:init")
    @ResponseBody
    public ResponseData addData(GweaT gweaT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gweaT.setGWEAENT(shiroUser.getDBC());
        if(gwei001Service.existedTheParams(gweaT)){
            return new ErrorResponseData(BizExceptionEnum.EXISTED_THE_PARAMS.getCode() ,
                    BizExceptionEnum.EXISTED_THE_PARAMS.getMessage());
        }
        gwei001Service.addData(gweaT);
        return SUCCESS_TIP;
    }

    /**
     * 修改数据
     */
    @RequestMapping("/editData")
    @RequiresPermissions("base:gwei001:init")
    @ResponseBody
    public ResponseData editData(GweaT gweaT,@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        GweaT oldGweaT = new GweaT();
        oldGweaT.setGWEAENT(gweaT.getGWEAENT());
        oldGweaT.setGWEA001(inDto.getString("oldGWEA001"));
        if(!gweaT.getGWEA001().equals(oldGweaT.getGWEA001())){
            if(gwei001Service.existedTheParams(gweaT)){
                return new ErrorResponseData(BizExceptionEnum.EXISTED_THE_PARAMS.getCode() ,
                        BizExceptionEnum.EXISTED_THE_PARAMS.getMessage());
            }
        }
        gwei001Service.editData(gweaT,oldGweaT);
        return SUCCESS_TIP;
    }

    /**
     * 删除数据
     */
    @RequestMapping("/deleteData")
    @RequiresPermissions("base:gwei001:init")
    @ResponseBody
    public ResponseData deleteData(GweaT gweaT) {
        gwei001Service.deleteData(gweaT);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/deleteDataBatch")
    @RequiresPermissions("base:gwei001:init")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GweaT> gweaTList = inDto.getReqList(GweaT.class);
        gwei001Service.deleteDataBatch(gweaTList);
        return SUCCESS_TIP;
    }

}
